﻿using System;
using System.Collections.Generic;
using System.Data;
using personalplaner.business.enums;
using personalplaner.business.stamm;
using personalplaner.business.dienstplan;

namespace personalplaner.dataprovider.dienstplan
{
	public partial class PlanDataService
	{
		private static IEnumerable<ArbeitsPlanHeader> m_ArbeitsPlanData(DataTable table)
		{
			var planidExists = table.Columns.Contains("planid");
			var atidExists = table.Columns.Contains("atid");
			var nameExists = table.Columns.Contains("name");
			var jahridExists = table.Columns.Contains("jahrid");
			var schemaidExists = table.Columns.Contains("schemaid");
			var vonExists = table.Columns.Contains("von");
			var bisExists = table.Columns.Contains("bis");
			var statusExists = table.Columns.Contains("status");
            var layoutExists = table.Columns.Contains("layout");

			foreach (DataRow row in table.Rows)
			{
				yield return new ArbeitsPlanHeader
				{
					ID = planidExists ? row.Field<int>("planid") : int.MaxValue,
					PlanId = planidExists ? row.Field<int>("planid") : 0,
					Name = nameExists ? row.Field<string>("name") : string.Empty,
					ArbeitsTageId = atidExists ? row.Field<int>("atid") : 0,
					JahrId = jahridExists ? row.Field<int>("jahrid") : 0,
					SchemaId = schemaidExists ? row.Field<int>("schemaid") : 0,
					Von = vonExists ? row.Field<DateTime>("von") : DateTime.MinValue,
					Bis = bisExists ? row.Field<DateTime>("bis") : DateTime.MinValue,
					Status = statusExists ? row.Field<int>("status") == 1 ? enStatus.Active : enStatus.Inactive : enStatus.Inactive,
                    Layout = layoutExists ? row.Field<string>("layout") : ""
				};
			}
		}

        private static IEnumerable<DienstPlanRowHeader> m_DienstPlanRowHeaderData(DataTable table)
        {
            var prsidExists = table.Columns.Contains("prsID");
            var nameExists = table.Columns.Contains("name");
            var nachnameExists = table.Columns.Contains("nachname");
            var abteilungidExists = table.Columns.Contains("abteilungid");
            var abteilungnameExists = table.Columns.Contains("abteilungname");
            var prsschemaidExists = table.Columns.Contains("prsschemaid");
            var vonExists = table.Columns.Contains("von");
            var bisExists = table.Columns.Contains("bis");
            var rgbExists = table.Columns.Contains("rgb");
            var pensumExists = table.Columns.Contains("pensum");
            var colornameExists = table.Columns.Contains("colorname");

            foreach (DataRow row in table.Rows)
            {
                yield return new DienstPlanRowHeader
                {
                    ID = prsidExists ? row.Field<int>("prsID") : int.MaxValue,
                    PrsId = prsidExists ? row.Field<int>("prsID") : 0,
                    Name = nameExists ? row.Field<string>("name") : string.Empty,
                    Nachname = nachnameExists ? row.Field<string>("nachname") : string.Empty,
                    AbteilungId = abteilungidExists ? row.Field<int>("abteilungid") : 0,
                    AbteilungeName = abteilungnameExists ? row.Field<string>("abteilungname") : string.Empty,
                    PrsSchemaId = prsschemaidExists ? row.Field<int>("prsschemaid") : -1,
                    Von = vonExists ? row.Field<DateTime>("von") : DateTime.MinValue,
                    Bis = bisExists ? row.Field<DateTime>("bis") : DateTime.MinValue,
                    Rgb = rgbExists ? row.Field<int>("rgb") : 0,
                    Pensum = pensumExists ? row.Field<int>("pensum") : 0,
                    ColorString = colornameExists ? row.Field<string>("colorname") : string.Empty
                };
            }
        }

        private static IEnumerable<PlanItem> m_DienstPlanRowData(DataTable table)
        {
            var datumExists = table.Columns.Contains("datum");
            var nameExists = table.Columns.Contains("name");
            var dienstidExists = table.Columns.Contains("dienstid");
            var stundenExists = table.Columns.Contains("stunden");
            var rgbExists = table.Columns.Contains("rgb");
            var colornameExists = table.Columns.Contains("colorname");
			var typExists = table.Columns.Contains("typ");

            foreach (DataRow row in table.Rows)
            {

				var diensttype = enDienstType.None;
				if (typExists)
				{
					try
					{
						diensttype = (enDienstType) row.Field<int>("typ");
					}
					catch
					{
					}
				}

            	yield return new PlanItem
                {
                    ID = int.MaxValue,
                    Name = nameExists ? row.Field<string>("name") : string.Empty,
                    Datum = datumExists ? row.Field<DateTime>("datum") : DateTime.Now,
                    DienstId = dienstidExists ? row.Field<int>("dienstid") : 0,
                    Stunden = stundenExists ? Convert.ToDouble(row.Field<decimal>("stunden")) : 0,
                    Argb = rgbExists ? row.Field<int>("rgb") : 0,
                    ColorString = colornameExists ? row.Field<string>("colorname") : string.Empty,
					DienstType = diensttype,
                };
            }
        }

		private static TimeSaldo m_TimeSaldoData(DataTable table)
		{
			var vortragExists = table.Columns.Contains("vortrag");
			var sollExists = table.Columns.Contains("soll");
			var habenExists = table.Columns.Contains("haben");
			var ferienExists = table.Columns.Contains("ferien");
			var anpassungExists = table.Columns.Contains("anpassung");

			if (table.Rows.Count <= 0)
				return null;

			var row = table.Rows[0] as DataRow;
			return new TimeSaldo
			{
				ID = int.MaxValue,
				Name = "TimeSaldo",
				Vortrag = vortragExists ? Convert.ToDouble(row.Field<decimal>("vortrag")) : 0.0,
				Soll = sollExists ? Convert.ToDouble(row.Field<decimal>("soll")) : 0.0,
				Haben = habenExists ? Convert.ToDouble(row.Field<decimal>("haben")) : 0.0,
				Ferien = ferienExists ? Convert.ToDouble(row.Field<decimal>("ferien")) : 0.0,
				Anpassung = anpassungExists ? Convert.ToDouble(row.Field<decimal>("anpassung")) : 0.0,
			};
		}

		private static IEnumerable<SaldoAnpassung> m_SaldoEditData(DataTable table)
		{
			var saldoidExists = table.Columns.Contains("saldoid");
			var datumExists = table.Columns.Contains("datum");
			var grundExists = table.Columns.Contains("grund");
			var stundenExists = table.Columns.Contains("stunden");
			var planidExists = table.Columns.Contains("planid");
			var abteilungidExists = table.Columns.Contains("abteilungid");
			var prsidExists = table.Columns.Contains("prsid");

			foreach (DataRow row in table.Rows)
			{
				yield return new SaldoAnpassung
				             {
				             	ID = saldoidExists?row.Field<int>("saldoid") : int.MaxValue,
				             	Name = "TimeSaldo",
								SaldoId = saldoidExists ? row.Field<int>("saldoid") : 0,
								Datum= datumExists ? row.Field<DateTime>("datum") : DateTime.MinValue,
								Reason= grundExists ? row.Field<string>("grund") : "",
								Amount = stundenExists ? Convert.ToDouble(row.Field<decimal>("stunden")) : 0.0,
								PlanId= planidExists ? row.Field<int>("planid") : 0,
								AbteilungId= abteilungidExists ? row.Field<int>("abteilungid") : 0,
								PrsId = prsidExists ? row.Field<int>("prsid") : 0
				             };
			}
		}

        private static IEnumerable<int> m_PersonenWithoutDienstData(DataTable table)
        {
            var prsidExists = table.Columns.Contains("prsid");

            foreach (DataRow row in table.Rows)
            {
                yield return prsidExists ? row.Field<int>("prsid") : 0;
            }
        }

        private static IEnumerable<PlanOverviewHeader> m_PlanOverviewHeadersData(DataTable table)
        {
            var abteilungidExists = table.Columns.Contains("abteilungid");
            var abteilungnameExists = table.Columns.Contains("abteilungname");
            var planidExists = table.Columns.Contains("planid");
            var plannameExists = table.Columns.Contains("planname");
            var monatExists = table.Columns.Contains("monat");
            var sollExists = table.Columns.Contains("soll");
            var habenExists = table.Columns.Contains("haben");
            var ferienExists = table.Columns.Contains("ferien");
            var anpassungExists = table.Columns.Contains("anpassung");
			var prsschemaidExists = table.Columns.Contains("prsschemaid");
			var vonExists = table.Columns.Contains("von");
			var bisExists = table.Columns.Contains("bis");

            foreach (DataRow row in table.Rows)
            {
				yield return new PlanOverviewHeader
				{
					ID = planidExists ? row.Field<int>("planid") : int.MaxValue,
					Name = "TimeSaldo",
					AbteilungId = abteilungidExists ? row.Field<int>("abteilungid") : 0,
					AbteilungName = abteilungnameExists ? row.Field<string>("abteilungname") : "",
					PlanId = planidExists ? row.Field<int>("planid") : 0,
					PlanName = plannameExists ? row.Field<string>("planname") : "",
					Monat = monatExists ? row.Field<string>("monat") : "",
					Soll = sollExists ? Convert.ToDouble(row.Field<decimal>("soll")) : 0.00,
					Haben = habenExists ? Convert.ToDouble(row.Field<decimal>("haben")) : 0.00,
					Ferien = ferienExists ? Convert.ToDouble(row.Field<decimal>("ferien")) : 0.00,
					Anpassung = anpassungExists ? Convert.ToDouble(row.Field<decimal>("anpassung")) : 0.00,
					PrsSchemaId = prsschemaidExists ? row.Field<int>("prsschemaid") : 0,
					Von = vonExists ? row.Field<DateTime>("von") : DateTime.MinValue,
					Bis = bisExists ? row.Field<DateTime>("bis") : DateTime.MinValue,
				};
            }
        }

		private static IEnumerable<ShortDienst> m_ShortDienstData(DataTable table)
		{
			var datumExists = table.Columns.Contains("datum");
			var dienstidExists = table.Columns.Contains("dienstid");
			var nameExists = table.Columns.Contains("name");

			foreach (DataRow row in table.Rows)
			{
				yield return new ShortDienst
				{
					ID = dienstidExists ? row.Field<int>("dienstid") : int.MaxValue,
					Name = nameExists ? row.Field<string>("name") : "",
					Date = datumExists ? row.Field<DateTime>("datum") : DateTime.MinValue,
					DienstId = dienstidExists ? row.Field<int>("dienstid") : 0
				};
			}
		}
	}
}
